package com.dabai.applicationmanager.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.dabai.applicationmanager.shizuku.Shell;
import com.dabai.applicationmanager.shizuku.ShizukuShell;
import com.dabai.applicationmanager.utils.OtherUtils;
import com.dabai.dbutils.toast.DBToast;
import com.dabai.dbutils.utils.ShellUtils;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

/**
 * Description : Other
 *
 * @author BAI
 */
public class ExecShellActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();

        String shell = intent.getStringExtra("shell");

        switch (OtherUtils.getImplementWay(this)) {
            case 1:
                DBToast.showText(this, "权限不足,请到设置更改执行方式!", false);
                finish();
                break;
            case 2:

                ShellUtils.CommandResult commandResult = ShellUtils.execCommand(shell, true);

                new MaterialAlertDialogBuilder(this)
                        .setTitle(commandResult.isSuccessful() ? "执行成功" : "执行失败")
                        .setMessage(commandResult.isSuccessful() ? commandResult.successMsg : commandResult.errorMsg)
                        .setOnDismissListener(dialog -> finish())
                        .setPositiveButton("确定", null)
                        .show().getWindow().setWindowAnimations(com.dabai.uitools.R.style.DBAnim_Transitions_Via);


                break;
            case 3:
                Shell.Result result = ShizukuShell.getInstance().exec(new Shell.Command(
                        shell));

                new MaterialAlertDialogBuilder(this)
                        .setTitle(result.isSuccessful() ? "执行成功" : "执行失败")
                        .setMessage(result.isSuccessful() ? result.out : result.err)
                        .setOnDismissListener(dialog -> finish())
                        .setPositiveButton("确定", null)
                        .show().getWindow().setWindowAnimations(com.dabai.uitools.R.style.DBAnim_Transitions_Via);

                break;
        }

        // finish();

    }
}
